
package org.sopt.dev.ImgKnight.monitor.dialog;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ConsoleDialog extends Dialog {

	private Display display = null;
	
	private Shell parent = null;
	
	private Shell shell = null;
	
	private Text consoleOut = null;
	
	private Text consoleIn = null;
	
	public ConsoleDialog(Display display, Shell parent) {
		this(display, parent, SWT.DEFAULT);
	}
	
	public ConsoleDialog(Display display, Shell parent, int style) {
		super(parent, style);
		this.display = display;
		this.parent = parent;
	}
	
	public void open() {
		shell = new Shell(parent, SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
		shell.setText("CrawlerMonitor");
		GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = gridLayout.marginWidth = 0;
		gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0;
		gridLayout.numColumns = 1;
		shell.setLayout(gridLayout);
		
		consoleOut = new Text(shell, SWT.WRAP|SWT.MULTI|SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
		consoleOut.setText("CrawlerMonitor Console On "+new Date()+
				"\r\nCopyright (c) 2009 sopt. All Rights Reserved.\r\n\r\n");
		consoleOut.setEditable(false);
		consoleOut.setBackground(new Color(display, 0, 0, 0));
		consoleOut.setForeground(new Color(display, 0, 255, 255));
		consoleOut.setFont(new Font(display, new FontData("Arial", 11, SWT.NONE)));
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gridData.widthHint = 550;
		gridData.heightHint = 350;
		consoleOut.setLayoutData(gridData);
		
		consoleIn = new Text(shell, SWT.WRAP|SWT.BORDER);
		consoleIn.setBackground(new Color(display, 0, 0, 0));
		consoleIn.setForeground(new Color(display, 0, 255, 0));
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gridData.widthHint = 570;
		gridData.heightHint = 18;
		gridData.verticalIndent = 2;
		consoleIn.setLayoutData(gridData);
		consoleIn.setFocus();
		consoleIn.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				if( e.character == SWT.CR ) {
					String in = consoleIn.getText();
					consoleIn.setText("");
					consoleOut.setText(consoleOut.getText()+in);
				}
			}
		});
		
		shell.pack();
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}
